<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>廖仙儿的后台相册管理系统</title>
    <div th:include="Introduce/head"></div>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">廖仙儿的后台相册管理系统</div>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <img src="http://t.cn/RCzsdCq" class="layui-nav-img">
                    <span class="x-red"  th:text="${session.user.userName}"/>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="javascript:window.location.href='/user/logout';">注销</a></li>
        </ul>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">

        </div>
    </div>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <iframe id="iframe" width="1150em" height="520em" th:src="@{/menu/indexMenu}"></iframe>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script th:inline="javascript">

    let menus = [];
    let afertId = [];
    //JavaScript代码区域
    layui.use('element', function () {
        let element = layui.element;
        //加载菜单
        $.ajax({
            url: '/menu/getMenuAll',
            async: false,
            success: function (res) {
                let topMenus = "<ul  class=\"layui-nav layui-layout-left\" lay-filter=\"filter\">";
                res.data.forEach(function (element, i) {
                    if (element.parentId == 0) {
                        topMenus += "<li class=\"layui-nav-item\"><a id='" + element.id + "'>" + element.menuName + "</a></li>\n"
                    } else {
                        menus.push(element)
                    }
                })
                topMenus += " <li class=\"layui-nav-item\">\n" +
                    "                    <a href=\"javascript:;\">其它系统</a>\n" +
                    "                    <dl class=\"layui-nav-child\">\n" +
                    "                        <dd><a>邮件管理</a></dd>\n" +
                    "                        <dd><a>消息管理</a></dd>\n" +
                    "                        <dd><a>授权管理</a></dd>\n" +
                    "                    </dl>\n" +
                    "                </li>\n" +
                    "</ul>";
                $('.layui-logo').after(topMenus);
                element.render('nav');
            }
        })

        //水平导航点击
        element.on('nav(filter)', function (elem) {
            $('.layui-side-scroll').children().remove();
            let leftMenus = "<ul id=\"left-filter\" class=\"layui-nav layui-nav-tree\"  lay-filter=\"left-filter\">"
            leftMenus += leftMenu(elem[0]);
            leftMenus += "</url>"
            $('.layui-side-scroll').append(leftMenus);
            for (let i = 0; i < afertId.length; i++) {
                let obj=$('#'+afertId[i]).parent().parent().get(0);
                if( obj.tagName=="DL") {

                    $('#' + afertId[i]).parent().after("<dd><a id='"+afertId[i]+"' href=\"javascript:;\">" + $('#' + afertId[i]).html() + "</a></dd>");
                }else {
                    $('#'+afertId[i]).parent().after("<li class=\"layui-nav-item\"><a id='"+afertId[i]+"' href=\"javascript:;\">"+$('#'+afertId[i]).html()+"</a></li>");
                }
                $('#'+afertId[i]).parent().remove();
            }
            element.render('nav');
        })
        //垂直导航点击
        element.on('nav(left-filter)', function (elem) {
            menus.forEach(function (ele){
                if(ele.id==elem[0].id){
                    if(ele.menuUrl!=''&&ele.menuUrl!=null){
                        $('#iframe').attr("src",ele.menuUrl)
                    }
                }
            })

        })

    });

    function leftMenu(elem) {
        let leftMenus = '';
        let isafter = false;
        for (let i = 0; i < menus.length; i++) {
            let ele = menus[i];
            if (ele.parentId == elem.id) {
                leftMenus += "<li  class=\"layui-nav-item\"><a id=" + ele.id + ">" + ele.menuName + "</a>" + " <dl  id=dl"+ele.id+ "  class=\"layui-nav-child\">\n";
                let cmenus = leftMenu(ele);
                if (cmenus != null && cmenus != '') {
                    leftMenus += cmenus;
                } else {
                    isafter = true
                }
                leftMenus += "</dl></li>"
                if (isafter) {
                    afertId.push(ele.id);
                    //$('#'+ele.id).parent().append("<dd><a href=\"javascript:;\">"+ele.menuName+"</a></dd>")
                }
            }
        }
        return leftMenus;
    }

</script>
</body>
</html>